#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"

SL::SL(int capacity)
{
	SLDateType* newArr = (SLDateType*)malloc(sizeof(SLDateType) * capacity);
	if (newArr == nullptr)
	{
		perror("malloc error!\n");
		return;
	}
	_capacity = capacity;
	_arr = newArr;
}
SL::~SL()
{
	if (_arr != NULL)
	{
		free(_arr);
		_arr = nullptr;
	}
	_capacity = _size = 0;
}

void SL::check()
{
	if (_size == _capacity)
	{
		int newCapacity = _size == 0 ? 4 : 2 * _size;
		SLDateType* newArr = (SLDateType*)realloc(_arr, sizeof(SLDateType) * newCapacity);
		if (newArr == nullptr)
		{
			perror("realloc error!\n");
			return;
		}
		_capacity = newCapacity;
		_arr = newArr;
	}
}
void SL::PushBack(int index)
{
	check();
	*(_arr + _size++) = index;
}
ostream& operator<<(ostream& out, SL& sl)
{
	for (int i = 0; i < sl._size; i++)
	{
		out << *(sl._arr + i) << " ";
	}
	out << endl;
	return out;
}
void SL::PushFront(int index)
{
	check();
	for (int i = _size; i > 0; i--)
	{
		*(_arr + i) = *(_arr + i-1);//*(arr+1) = *(arr+0);
	}
	*_arr = index;
	++_size;
}
void SL::PopBack()
{
	assert(_size);
	--_size;
}
void SL::PopFront()
{
	assert(_size);
	for (int i = 0; i < _size; i++)
	{
		*(_arr + i) = *(_arr + i + 1);
	}
	--_size;
}
void SL::Insert(int pos,int index)
{
	assert(pos >= 0 && pos <= _size);
	check();
	for (int i = _size; i > pos; i--)
	{
		*(_arr + i) = *(_arr + i - 1);//
	}
	*(_arr + pos) = index;
	++_size;
}
void SL::Erase(int pos)
{
	assert(pos >= 0 && pos < _size);
	for (int i = pos; i < _size; i++)
	{
		*(_arr + i) = *(_arr + i + 1);
	}
	--_size;
}
int SL::Find(int index)
{
	assert(_size);
	for (int i = 0; i < _size; i++)
	{
		if (*(_arr + i) == index)
		{
			return i;
		}
	}
	return -1;
}
